import java.util.Collection;
import java.util.HashSet;
import java.util.function.Predicate;

public class PredicateTest2 {
    public static void main(String[] args) {
        // 创建一个集合
        HashSet books = new HashSet();
        books.add("Java集合");
        books.add("数据结构与算法");
        books.add("Java从入门到精通");
        books.add("C++从入门到精通");

        System.out.println(calAll(books, ele -> ((String) ele).contains("精通")));
        System.out.println(calAll(books, ele -> ((String) ele).contains("Java")));
        System.out.println(calAll(books, ele -> ((String) ele).length() > 9));
    }

    public static int calAll(Collection books, Predicate p) {
        int total = 0;
        for (Object book : books) {
            // 使用Predicate的test()方法判断该对象是否满足Predicate指定的条件
            if (p.test(book)) {
                total++;
            }
        }
        return total;
    }
}
